package com.daffodil.workable.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.daffodil.core.annotation.AuthPermission;
import com.daffodil.core.annotation.DictConver;
import com.daffodil.core.annotation.Log;
import com.daffodil.core.annotation.Log.BusinessType;
import com.daffodil.core.controller.ReactiveBaseController;
import com.daffodil.core.entity.JsonResult;
import com.daffodil.core.entity.Page;
import com.daffodil.core.entity.TableInfo;
import com.daffodil.workable.constant.WorkableConstant;
import com.daffodil.workable.controller.model.WorkableTaskParam;
import com.daffodil.workable.entity.WorkableTask;
import com.daffodil.workable.service.IWorkableTaskService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;
import reactor.core.publisher.Mono;

/**
 * -计划任务控制层
 * @author yweijian
 * @date 2022-04-06
 * @version 1.0
 * @description
 */
@Api(value = "计划任务管理", tags = "计划任务管理")
@RestController
@RequestMapping(WorkableConstant.API_CONTENT_PATH + "/task")
public class WorkableTaskController extends ReactiveBaseController {

    @Autowired
    private IWorkableTaskService workableTaskService;

    @ApiOperation("分页查询计划任务列表")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @SuppressWarnings("unchecked")
    @DictConver
    @AuthPermission("workable:task:list")
    @GetMapping("/list")
    public Mono<TableInfo> list(WorkableTask workableTask, Page page, @ApiIgnore ServerHttpRequest request) {
        initQuery(workableTask, page, request);
        List<WorkableTask> list = workableTaskService.selectWorkableTaskList(query);
        return initTableInfo(list, query).getMono();
    }

    @ApiOperation("获取计划任务详情")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @GetMapping("/info")
    public Mono<JsonResult> info(@ApiParam(value = "计划任务ID") String id, @ApiIgnore ServerHttpRequest request) {
        WorkableTask workableTask = workableTaskService.selectWorkableTaskById(id);
        return JsonResult.success(workableTask).getMono();
    }

    @ApiOperation("新增计划任务")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @AuthPermission("workable:task:add")
    @Log(title = "计划任务管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Mono<JsonResult> add(@Validated @RequestBody WorkableTask workableTask, @ApiIgnore ServerHttpRequest request) {
        workableTaskService.insertWorkableTask(workableTask);
        return JsonResult.success(workableTask).getMono();
    }

    @ApiOperation("修改计划任务")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @AuthPermission("workable:task:edit")
    @Log(title = "计划任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Mono<JsonResult> edit(@Validated @RequestBody WorkableTask workableTask, @ApiIgnore ServerHttpRequest request) {
        workableTaskService.updateWorkableTask(workableTask);
        return JsonResult.success(workableTask).getMono();
    }

    @ApiOperation("删除计划任务")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @AuthPermission("workable:task:remove")
    @Log(title = "计划任务管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public Mono<JsonResult> remove(@RequestBody String[] ids, @ApiIgnore ServerHttpRequest request) {
        workableTaskService.deleteWorkableTaskByIds(ids);
        return JsonResult.success().getMono();
    }
    
    @ApiOperation("部署计划任务")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @AuthPermission("workable:task:deploy")
    @Log(title = "计划任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/deploy")
    public Mono<JsonResult> deploy(@RequestBody WorkableTaskParam params, @ApiIgnore ServerHttpRequest request) {
        workableTaskService.deployWorkableTaskByIds(params.getIds(), params.getVersion());
        return JsonResult.success().getMono();
    }
    
    @ApiOperation("作废计划任务")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @AuthPermission("workable:task:trash")
    @Log(title = "计划任务管理", businessType = BusinessType.TRASH)
    @PostMapping("/trash")
    public Mono<JsonResult> trash(@RequestBody String[] ids, @ApiIgnore ServerHttpRequest request) {
        workableTaskService.trashWorkableTaskByIds(ids);
        return JsonResult.success().getMono();
    }
    
    @ApiOperation("执行计划任务")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @AuthPermission("workable:task:run")
    @Log(title = "计划任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/run")
    public Mono<JsonResult> run(@RequestBody WorkableTaskParam params, @ApiIgnore ServerHttpRequest request) {
        workableTaskService.runWorkableTaskByIds(params.getIds(), params.getRunmode());
        return JsonResult.success().getMono();
    }

}
